package com.example.phone.account.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.phone.R;
import com.example.phone.account.activity.AddAddressActivity;
import com.example.phone.base.ActivityCollector;
import com.example.phone.base.BaseActivity;
import com.example.phone.base.MyApp;
import com.example.phone.bean.AddressItem;
import com.example.phone.bean.Constants;

import java.util.List;

/**
 * Created by Administrator on 2018\12\26 0026.
 */

public class AddressRecycleAdapter extends RecyclerView.Adapter<AddressRecycleAdapter.ViewHolder> {

    private List<AddressItem> addressItems;
    private Context context;
    private int shippingId;
    static class ViewHolder extends RecyclerView.ViewHolder {
        View addressItemView;
        TextView tv_username;
        TextView receiver_name;
        TextView telephone_number;
        TextView address;
        TextView edit_address;
        LinearLayout ll_edit_address;


        public ViewHolder(View view) {
            super(view);
            addressItemView = view;
            //tv_username = (TextView) view.findViewById(R.id.tv_username);
            receiver_name = (TextView) view.findViewById(R.id.receiver_name);
            telephone_number = (TextView) view.findViewById(R.id.telephone_number);
            address = (TextView) view.findViewById(R.id.address);
            edit_address = (TextView) view.findViewById(R.id.edit_address);
            ll_edit_address = view.findViewById(R.id.ll_address_edit);
        }
    }

    public AddressRecycleAdapter(Context context, List<AddressItem> addressItems) {
        this.addressItems = addressItems;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final AddressItem addressItem = addressItems.get(position);
        shippingId = addressItem.getShippingId();
        Log.d("AddressAdapter", "shippingId = "+shippingId+"");
     //   holder.tv_username.setText(addressItem.getUserName());
        holder.receiver_name.setText(addressItem.getName());
        Log.d("AddressRecycleAdapter",addressItem.getName());
        holder.telephone_number.setText(addressItem.getPhone());
        holder.address.setText(addressItem.getFullAddress());
        holder.ll_edit_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //跳转到 地址编辑 页面
                Intent intent = new Intent(context, AddAddressActivity.class);
                //传递地址id到编辑界面
                intent.putExtra("shippingId", addressItem.getShippingId());
                //传递要跳转的页面的
                intent.putExtra("page", Constants.EDIT_ADDRESS);
                Bundle bundle = new Bundle();
                bundle.putString("receiver", addressItem.getName());
                bundle.putString("telephone", addressItem.getPhone());
                bundle.putString("address", addressItem.getFullAddress());
                intent.putExtra("bundle", bundle);
                context.startActivity(intent);


            }
        });

    }

    @Override
    public int getItemCount() {
        return addressItems.size();
    }
}
